/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaCategoriaDeVinculo.java
 *
 * Created on 07/11/2010, 11:02:46
 */

package asfabdesk.apresentacao;

import asfabdesk.dominio.CategoriaVinculo;
import asfabdesk.presistencia.DAO;
import javax.swing.JOptionPane;

/**
 *
 * @author Mathias
 */
public class TelaCategoriaDeVinculo extends javax.swing.JInternalFrame {
    private CategoriaVinculo categoria = new CategoriaVinculo();

    /** Creates new form TelaCategoriaDeVinculo */
    public TelaCategoriaDeVinculo() {
        initComponents();
    }

    public void salvar() {
        if (verificarCampo()) {
            if (verificarDup()) {
                try {
                    categoria = povoarCategoria();
                    new DAO().salvar(categoria);
                    JOptionPane.showMessageDialog(null, "Cadastro efetuado com Sucesso'.", "Sucesso!", 1);
                    limpar();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Ops, ocorreu um erro durante o cadastro'.", "Ops,erro!", 0);
                }
            }
        }
    }

    public boolean verificarCampo() {
        boolean retorno = true;
        if (txtCategoria.getText().equals("")) {
            retorno = false;
        }
        if (retorno == false) {
            JOptionPane.showMessageDialog(null, "Ops, você não preencheu o campo secretaria", "Campos Obrigatórios.", 2);
        }
        return retorno;
    }

    public boolean verificarDup() {
        boolean r = true;
        if (new DAO().localizar(CategoriaVinculo.class, "CategoriaVinculoPorNome", txtCategoria.getText().toUpperCase()) != null) {
            r = false;
        }
        if (r == false) {
            JOptionPane.showMessageDialog(null, "Ops, você está tentando salvar uma Categoria já cadastrada", "Ops.", 2);
        }
        return r;
    }

    public void limpar() {
        txtCategoria.setText("");
        txtSigla.setText("");
    }

    public CategoriaVinculo povoarCategoria() {
        CategoriaVinculo cat = new CategoriaVinculo();
        cat.setDescricao(txtCategoria.getText().toUpperCase());
        cat.setSigla(txtSigla.getText().toUpperCase());
        return cat;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtCategoria = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtSigla = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnAdicionar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(asfabdesk.apresentacao.AsfabDeskApp.class).getContext().getResourceMap(TelaCategoriaDeVinculo.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setBackground(resourceMap.getColor("jPanel2.background")); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel2.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 0, -1, -1));

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setIcon(resourceMap.getIcon("jLabel2.icon")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel2.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 100, 90));

        txtCategoria.setText(resourceMap.getString("txtCategoria.text")); // NOI18N
        txtCategoria.setName("txtCategoria"); // NOI18N
        jPanel2.add(txtCategoria, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 40, 230, -1));

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 20, -1, -1));

        txtSigla.setText(resourceMap.getString("txtSigla.text")); // NOI18N
        txtSigla.setName("txtSigla"); // NOI18N
        jPanel2.add(txtSigla, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 90, 50, -1));

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        jPanel2.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 70, -1, -1));

        btnAdicionar.setText(resourceMap.getString("btnAdicionar.text")); // NOI18N
        btnAdicionar.setName("btnAdicionar"); // NOI18N
        btnAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdicionarActionPerformed(evt);
            }
        });
        jPanel2.add(btnAdicionar, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 90, -1, 20));

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, 130));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 390, 150));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdicionarActionPerformed
        salvar();
    }//GEN-LAST:event_btnAdicionarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdicionar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtCategoria;
    private javax.swing.JTextField txtSigla;
    // End of variables declaration//GEN-END:variables

}
